package com.vietthangif.project.contact;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;

public class Contact {
	private final static String TAG = Contact.class.getName();
	public final static String LOAD_CONTACT_ERROR = TAG + ".LOAD_CONTACT_ERROR";
	public final static String NUM_CONTACT_ZERO = TAG + ".NUM_CONTACT_ZERO";
	public final static String PHONE_CONTACT_ERROR = TAG + ".PHONE_CONTACT_ERROR";
	
	public static Cursor getContacts(Context context) {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";
        return context.getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
	}
	
	public static ArrayList<Entry> loadContact(Context context) {
		Cursor cursor = getContacts(context);
		Cursor phoneCur = null;
		ArrayList<Entry> contacts = new ArrayList<Entry>();
		if(cursor == null) {
			Log.e(TAG, "cursor return null");
			Intent loadError = new Intent(LOAD_CONTACT_ERROR);
			context.sendBroadcast(loadError);
			return null;
		} else if (cursor.getCount() < 1) {
			Log.d(TAG, "number contacts zero");
			Intent loadZero = new Intent(NUM_CONTACT_ZERO);
			context.sendBroadcast(loadZero);
			return null;
		} else {
	        while (cursor.moveToNext()) {
	            String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
	            
	            //find numberPhone, email with each contact
	            Log.d(TAG, "contact: " + displayName + ",contact id: "+ cursor.getString(cursor.getColumnIndex(ContactsContract.Data._ID)));
	            String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + cursor.getString(cursor.getColumnIndex(ContactsContract.Data._ID));
	            Log.d(TAG,"selection: " + selection);
	            phoneCur = context.getContentResolver().query( 
	    		        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, selection, null, null); 
	    	    if(phoneCur == null) {
	    	    	Log.e(TAG, "phone of " + displayName +" return null");
	    	    	Intent loadError = new Intent(PHONE_CONTACT_ERROR);
	    	    	context.sendBroadcast(loadError);
	    	    	continue;
	    	    } else
	    	    if(phoneCur.getCount() == 0) {
	    	    	String phoneNumber = "No phone";
	    	    	Entry contact = new Entry(displayName, phoneNumber);
	    	    	contacts.add(contact);
	    	    } else {
		            while (phoneCur.moveToNext()) { 
		    	        String phoneNumber = phoneCur.getString(
		    	                      phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
		    	        Log.d(TAG, "phone number: " + phoneNumber + ",phone id: " + phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
		    	        Entry contact = new Entry(displayName, phoneNumber);
		    	        contacts.add(contact);
		    	    }
	    	    }
	        }
		}
		
	    cursor.close();
	    phoneCur.close();
		
        return contacts;
	}

}
